<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="16">
          <!-- 用户名输入框 -->
          <el-input
            v-model="searchModel.username"
            placeholder="用户名"
            clearable
          ></el-input>
          <!-- 活动名称输入框 -->
          <el-input
            v-model="searchModel.eventName"
            placeholder="活动名称"
            clearable
          ></el-input>
          <!-- 状态选择框 -->
          <el-select
            v-model="searchModel.status"
            placeholder="状态"
            clearable
            style="width: 200px; margin-left: 10px"
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option label="待审核" :value="0"></el-option>
            <el-option label="已通过" :value="1"></el-option>
            <el-option label="已拒绝" :value="2"></el-option>
            <el-option label="已取消" :value="3"></el-option>
          </el-select>
          <!-- 查询按钮 -->
          <el-button
            type="primary"
            round
            icon="el-icon-search"
            @click="getRegistrationList"
            style="margin-left: 10px"
          >
            查询
          </el-button>
        </el-col>
        <el-col :span="8" align="right">
          <el-button
            type="success"
            @click="batchApprove"
            :disabled="selectedRegistrations.length === 0"
            style="margin-right: 10px"
          >
            批量同意
          </el-button>
          <el-button
            type="danger"
            @click="batchReject"
            :disabled="selectedRegistrations.length === 0"
          >
            批量拒绝
          </el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table
        :data="registrationList"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
          :selectable="isSelectable"
        ></el-table-column>
        <el-table-column
          prop="id"
          label="ID"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="userName"
          label="姓名"
          width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="eventName"
          label="活动名称"
          width="250"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createdAt"
          label="创建时间"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            {{ formatDate(scope.row.createdAt) }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 确认对话框 -->
    <el-dialog
      title="确认操作"
      :visible.sync="dialogVisible"
      width="30%"
      @close="dialogVisible = false"
    >
      <span>{{ dialogMessage }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmBatchAction">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

  
<script>
import registrationApi from "@/api/registrationManage"; // 替换为报名相关 API 路径

export default {
  data() {
    return {
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
        username: "",
        eventName: "",
        status: null, // 新增状态字段
      },
      registrationList: [],
      selectedRegistrations: [], // 存储选中项
      dialogVisible: false, // 控制确认对话框的显示
      batchAction: null, // 当前执行的批量操作（同意或拒绝）
      dialogMessage: "", // 对话框提示信息
    };
  },
  mounted() {
    this.getRegistrationList();
  },
  methods: {
    isSelectable(row) {
      // 仅允许状态为待审核的行可选
      return row.status === 0;
    },
    getStatusText(status) {
      switch (status) {
        case 0:
          return "待审核";
        case 1:
          return "已通过";
        case 2:
          return "已拒绝";
        case 3:
          return "已取消";
        default:
          return "未知状态";
      }
    },
    getStatusType(status) {
      switch (status) {
        case 0:
          return "warning"; // 待审核
        case 1:
          return "success"; // 已通过
        case 2:
          return "danger"; // 已拒绝
        case 3:
          return "info"; // 已取消
        default:
          return ""; // 默认样式
      }
    },
    formatDate(value) {
      if (!value) return "-";
      return value.replace("T", " "); // 将 'T' 替换为空格
    },
    handleSelectionChange(selection) {
      this.selectedRegistrations = selection;
    },
    batchApprove() {
      if (this.selectedRegistrations.length === 0) {
        this.$message({
          type: "warning",
          message: "请选择需要操作的记录！",
        });
        return;
      }
      this.batchAction = "approve";
      this.dialogMessage = "确认批量同意选中的报名吗？";
      this.dialogVisible = true;
    },
    batchReject() {
      if (this.selectedRegistrations.length === 0) {
        this.$message({
          type: "warning",
          message: "请选择需要操作的记录！",
        });
        return;
      }
      this.batchAction = "reject";
      this.dialogMessage = "确认批量拒绝选中的报名吗？";
      this.dialogVisible = true;
    },
    confirmBatchAction() {
      const ids = this.selectedRegistrations.map((item) => item.id);
      if (this.batchAction === "approve") {
        registrationApi.batchApprove(ids).then(() => {
          this.$message({
            type: "success",
            message: "批量同意成功！",
          });
          this.getRegistrationList();
        });
      } else if (this.batchAction === "reject") {
        registrationApi.batchReject(ids).then(() => {
          this.$message({
            type: "success",
            message: "批量拒绝成功！",
          });
          this.getRegistrationList();
        });
      }
      this.dialogVisible = false; // 关闭对话框
    },
    handleSizeChange(newSize) {
      this.searchModel.pageSize = newSize;
      this.searchModel.pageNo = 1;
      this.getRegistrationList();
    },
    handleCurrentChange(newPage) {
      this.searchModel.pageNo = newPage;
      this.getRegistrationList();
    },
    getRegistrationList() {
      registrationApi
        .getRegistration(this.searchModel)
        .then((response) => {
          this.total = response.data.total;
          this.registrationList = response.data.list;
        })
        .catch((error) => {
          console.error("获取报名列表失败", error);
        });
    },
  },
};
</script>

  
  <style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
#search .el-select {
  width: 200px;
}
</style>